package com.oracle.zebrafreelytraveling.ui.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.map.MapUtil;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.CommonAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.MultiItemTypeAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ViewHolder;

import java.util.List;

/**
 * Created by yydcnjjw on 2017/8/8.
 */

public class BusSlideDailogFragment extends SlideBottomDialogFragment {

    public static final String DATA = "com.oracle.zebrafreelytraveling.ui.fragment.BusSlideDailogFragment.data";

    private RecyclerView mBusResultRecycler;
    private CommonAdapter<BusPath> mBusResuleAdapter;
    private List<BusPath> mBusPathList;

    private BusRouteResult mBusRouteResult;

    private BusRouteListener busRouteListener;

    public static BusSlideDailogFragment newInstance(BusRouteResult routeResult) {
        BusSlideDailogFragment f = new BusSlideDailogFragment();
        Bundle args = new Bundle();
        args.putParcelable(DATA, routeResult);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        busRouteListener = (BusRouteListener) activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        getBundleData();
        initView();
        return mView;
    }

    private void getBundleData(){
        Bundle args = getArguments();
        mBusRouteResult = args.getParcelable(DATA);
        assert mBusRouteResult != null;
        mBusPathList = mBusRouteResult.getPaths();
        Log.d("mBusPathList", mBusPathList.size()+"");
    }

    private void initView(){
        mBusResultRecycler = (RecyclerView) mView.findViewById(R.id.navi_bus_result_list);
        mBusResultRecycler.setItemAnimator(new DefaultItemAnimator());
        mBusResultRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBusResuleAdapter = new CommonAdapter<BusPath>(getActivity(), R.layout.item_navi_bus_result, mBusPathList) {
            @Override
            protected void convert(ViewHolder holder, BusPath path, int position) {
                Log.d("bus_path_title", MapUtil.getBusPathTitle(path));
                holder.setText(R.id.bus_path_title, MapUtil.getBusPathTitle(path));
                holder.setText(R.id.bus_path_des, MapUtil.getBusPathDes(path));
            }
        };
        mBusResuleAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                NaviSlideDialogFragment.newInstance(mBusPathList.get(position)).showDialogFragment(getFragmentManager());
                busRouteListener.setBusRouteToMap(mBusPathList.get(position), mBusRouteResult);
                dismissAllowingStateLoss();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mBusResultRecycler.setAdapter(mBusResuleAdapter);
    }

    public void showDialogFragment(FragmentManager fragmentManager){
        show(fragmentManager, "BUS_SLIDE_DIALOGFRAGMENT");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_navi_bus_result;
    }

    /**
     * 设置bus的线路监听
     */
    public interface BusRouteListener{
        void setBusRouteToMap(BusPath path, BusRouteResult mBusRouteResult);
    }

}
